#coding:utf-8
import sys
import os
import time
curPath = os.path.abspath(os.path.dirname(__file__))
rootPath = os.path.split(curPath)[0]
rootPath = os.path.split(rootPath)[0]
sys.path.append(rootPath)
import matplotlib
matplotlib.use('Agg')
from matplotlib import pyplot
from matplotlib import  rcParams,rcdefaults
from matplotlib.font_manager import  FontProperties
from getdata import GetData

# from testCases.fast_app.setting import  *
reload(sys)
sys.setdefaultencoding('utf8')

class DraWing(object):
    """docstring for DraWing"""
    def __init__(self):
        self.GD = GetData()
        self.rootdir = os.getcwd()
        self.weblist = self.GD.put_list()

    def get_number_list(self,filename):
        s ={}
        l =[]
        with open(filename,'r') as f:
           string= f.read().split(':')
           speedIndex=string[2].split(' ') [0]
           totalTime =string[3]
           number_l =string[1].split(',')
           number_l[0]=number_l[1]
           number_l[len(number_l)-1]=1
        for i in number_l[:-1]:
            l.append(float(i.strip()))
        l.append(1.0)
        s['number_list']=l
        s['speedIndex']=speedIndex
        s['totalTime']=totalTime
        return s

    def putimage(self,s,s1,s2,web_name):
        rcdefaults()
        p = rcParams
        p["figure.figsize"] = (20, 8.15)
        pyplot.rcParams['font.family']='Microsoft YaHei'
        ax=pyplot.gca()
        ax.yaxis.grid(linestyle='--',linewidth=1,alpha=0.2)
        x = []
        x1 = []
        x2 = []
        y = s['number_list']
        y1 = s1['number_list']
        y2 = s2['number_list']
        try:
            for i in range(0,350*16,16):
                x.append(i)
                x1.append(i)
                x2.append(i)
                if i >= len(y)*16:
                    y.append(0)
                if i >= len(y1)*16:
                    y1.append(0)
                if i >= len(y2)*16:
                    y2.append(0)
        except Exception, e:
                print e

        pyplot.plot(x, y,color='red',alpha=0.3)
        pyplot.plot(x1, y1, color='blue',alpha=0.3)
        pyplot.plot(x2, y2, color='yellow',alpha=0.3)
        pyplot.yticks([0, 0.1, 0.2, 0.3, 0.4,0.5,0.6,0.7,0.8,0.9,1.0])
        pyplot.xticks(range(0, 350*16, 5*16),rotation=90)
        
        pyplot.fill_between(x, y, color='red',alpha=0.3)
        pyplot.fill_between(x1, y1, color='blue',alpha=0.3)
        pyplot.fill_between(x2, y2, color='yellow',alpha=0.3)

        pyplot.text(180, 0.860, '.', fontsize=138,color='red')
        pyplot.xlabel(u'单位时间ms',fontsize=22,)
        pyplot.text(280,0.860,u'---Hwbrowser',fontsize=18,)
        pyplot.text(200, 0.78, 'totalTime:%s'%s['totalTime'], fontsize=16,)
        pyplot.text(200, 0.73, 'speedIndex:%s' % s['speedIndex'], fontsize=16,)

        pyplot.text(180, 0.660, '.', fontsize=138, color='blue')
        pyplot.text(280, 0.660, u'---Chrome', fontsize=18)
        pyplot.text(200, 0.58, 'totalTime:%s' % s1['totalTime'], fontsize=16,)
        pyplot.text(200, 0.53, 'speedIndex:%s' % s1['speedIndex'], fontsize=16,)

        pyplot.text(180, 0.460, '.', fontsize=138, color='yellow')
        pyplot.text(280, 0.460, u'---UCbrowser', fontsize=18)
        pyplot.text(200, 0.38, 'totalTime:%s' % s2['totalTime'], fontsize=16,)
        pyplot.text(200, 0.33, 'speedIndex:%s' % s2['speedIndex'], fontsize=16,)
        pyplot.title(u"www.%s网站启动加载体验对比"%web_name , fontsize=24,)

        pyplot.savefig(os.path.join(self.rootdir,'pngfile',web_name+'.png'))
        pyplot. close('all') 
 
    def generate(self,web=''):
        try:
            for web in self.weblist:
                print web
                h=self.get_number_list(os.path.join(self.rootdir,'txtfile',web+'_huawei.txt'))
                c=self.get_number_list(os.path.join(self.rootdir,'txtfile',web+'_chrome.txt'))
                u=self.get_number_list(os.path.join(self.rootdir,'txtfile',web+'_uc.txt'))
                self.putimage(h,c,u,web)
        except Exception, e:
            raise e

if  __name__=='__main__':
    dw = DraWing()
    dw.generate()


